<template>
    <el-dialog title="配置详情" :visible.sync="show" width="30%">
        <el-form v-if="type_==1">
            <el-form-item label="item.label">
                <el-input :placeholder="1" v-model="a"></el-input>
            </el-form-item>
        </el-form>
        <div v-if="type_==2">
            <div v-for="(item, index) in template" class="eidtItem" :key="index">
                <el-input v-model="item.label" style="margin-right: 20px"></el-input>
                <el-input v-model="item.account" style="margin-right: 20px"></el-input>
                <el-input v-model="item.password"></el-input>
                <i style="font-size: 28px; width: 50px; text-align: center; color: #007bff; margin: auto 0" v-if="i !== 0" @click="deleteRow(index)" class="pointer cursor el-icon-circle-close"></i>
            </div>
            <div class="pointer add" @click="addRow()">新增账号</div>
        </div>
        <div slot="footer">
            <el-button @click="show = false">取 消</el-button>
            <el-button type="primary" @click="submitForm">保 存</el-button>
        </div>
    </el-dialog>
</template>
<script>
export default {
    data() {
        return {
            show: false,
            type_ :0
        }
    },
    props: {
        // template: {
        //     type: Array,
        //     default: () => {
        //         return []
        //     },
        // },
    },
    methods: {
        getType(val) {
            function isJSONString(str) {
                try {
                    JSON.parse(str)
                    return true
                } catch (e) {
                    return false
                }
            }
            if (typeof val === 'string') {
                if (isJSONString(val)) {
                    return 1
                } else {
                    return 2
                }
            } else {
                return -1
            }
        },
        openDialog(val) {
            this.type_ = this.getType(val.value)
            this.show = true
        },
        closeDialog() {
            this.show = false
        },
        addRow() {
            this.template.push({
                label: '',
                value: '',
            })
        },
        deleteRow(index) {
            this.template.splice(index, 1)
        },
        submitForm() {
            this.$emit('submit', this.template)
        },
    },
}
</script>

<style lang="less" scoped>
.eidtItem {
    display: flex;
    margin-bottom: 14px;
    .el-input {
        flex: 1;
    }
}
.add {
    font-size: 18px;
    width: calc(100% - 52px);
    border-radius: 4px;
    text-align: center;
    height: 40px;
    line-height: 40px;
    color: #007bff;
    border: #007bff solid 1px;
}
</style>
